<?php
	//链接数据库
	$link= mysqli_connect("127.0.0.1", "root", "", "demo");
	
	//get请求，呈现数据
	if(isset($_GET['id']) && !empty($_GET['id'])){
		//接收id
		$id=$_GET['id'];	

		//查询数据库
		$sql="select * from users where id=$id";
		$result = mysqli_query($link,$sql);
		$result = mysqli_fetch_row($result);

	}	

	
	//post请求  修改数据
	if(isset($_POST['username']) && !empty($_POST['username'])){
		//接收数据
		$id=$_POST['id'];
		$username=$_POST['username'];
		$age=$_POST['age'];
		$sex=$_POST['sex'];

		//准备sql
		$sql="update users set username='$username',age=$age,sex=$sex where id=$id";

		$result=mysqli_query($link,$sql);
		//如果修改成功，跳转列表页
		if($result){
			header("location:/demo/list.php");
		}
	}

?>

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<h1>修改</h1>
	<form action="./update.php" method="post">

			<div>
				<label for="">用户名</label>
				<input type="text" name="username" value="<?php echo $result[1];?>" />
				<!-- 隐藏域 -->
				<input type="hidden"  name="id" value="<?php echo $id;?>">
			</div>
			<div id="">
				<label for="">年龄</label>
				<input type="text" name="age" value="<?php echo $result[2]?>" />
			</div>
			<div>
				<label for="">性别</label>
				<input type="radio" name="sex" <?php if($result[3]==1){echo "checked";}?> value="1" />男
				<input type="radio" <?php if($result[3]==2){echo "checked";}?> name="sex" value="2" />女
			</div>
			<button>提交</button>
		</form>		
</body>
</html>